import time
import sys
import random
from datetime import datetime

def print_progress_bar(stage, total, current):
    # 计算进度条的长度，随着总进步增加，进度条总长度也增加
    bar_length = int(current / total * 30) if current != 0 else 0  # 假设进度条长度为30
    bar = "=" * bar_length + ">" + "-" * (30 - bar_length - 1)

    # 随机生成 y 的值，确保 y 不超过12
    y = random.randint(0, 12)
    x = current - y
    x = max(0, x)  # 确保 x 不为负数

    # 打印进度条和当前进度
    sys.stdout.write(f"\r\033[31m[Stage {stage}:{bar}]{' ' * 5}({x} + {y}) / {total}\033[0m")
    sys.stdout.flush()

def clear_progress_bar():
    # 清除当前行的内容
    sys.stdout.write("\r" + " " * 50 + "\r")  # 用空格覆盖进度条
    sys.stdout.flush()

def run_progress_bars(num_stages, max_total):
    # 打印红色字体的固定内容，仅打印一次
    print("\033[31mUsing Spark's default log4j profile: org/apache/spark/log4j-defaults.properties\033[0m")
    print("\033[31mSetting default log level to \"WARN\".\033[0m")
    print(
        "\033[31mTo adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel).\033[0m")
    # 获取当前时间
    current_time = datetime.now().strftime("%d/%m/%y %H:%M:%S")
    # 打印包含当前时间的日志信息
    print(
        f"\033[31m{current_time} WARN Client: Neither spark.yarn.jars nor spark.yarn.archive is set, falling back to uploading libraries under SPARK_HOME.\033[0m")

    # 等待半分钟
    time.sleep(30)
    for stage in range(num_stages):
        total = random.randint(1, max_total)  # 随机生成总进度，不大于max_total
        current = 0  # 当前进度

        # 模拟进度条
        while current <= total:
            print_progress_bar(stage, total, current)
            current += 1
            time.sleep(0.1)  # 控制进度条更新速度

        # 清除进度条显示
        clear_progress_bar()

    # 打印换行，避免后续输出覆盖进度条
    print()

if __name__ == "__main__":
    run_progress_bars(12, 400)